


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > MultiTreeNode</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">net.mooctest</a>
</div>

<h1>Coverage Summary for Class: MultiTreeNode (net.mooctest)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">MultiTreeNode</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    20%
  </span>
  <span class="absValue">
    (1/5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    2.5%
  </span>
  <span class="absValue">
    (1/40)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package net.mooctest;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;
<i class="no-highlight">4</i>&nbsp;import java.util.Collection;
<i class="no-highlight">5</i>&nbsp;import java.util.Collections;
<i class="no-highlight">6</i>&nbsp;import java.util.HashSet;
<i class="no-highlight">7</i>&nbsp;
<i class="no-highlight">8</i>&nbsp;/**
<i class="no-highlight">9</i>&nbsp; * This class represents the K-ary (multiple node) tree data
<i class="no-highlight">10</i>&nbsp; * structure
<i class="no-highlight">11</i>&nbsp; * &lt;h1&gt;Definition&lt;/h1&gt;
<i class="no-highlight">12</i>&nbsp; * &lt;p&gt;
<i class="no-highlight">13</i>&nbsp; * &lt;b&gt;K-ary tree&lt;/b&gt; - tree, in which each node has no more than k subtrees
<i class="no-highlight">14</i>&nbsp; *
<i class="no-highlight">15</i>&nbsp; * @author shell
<i class="no-highlight">16</i>&nbsp; * @version 1.0.0
<i class="no-highlight">17</i>&nbsp; * @since 1.0.0
<i class="no-highlight">18</i>&nbsp; */
<i class="no-highlight">19</i>&nbsp;public abstract class MultiTreeNode&lt;T&gt; extends TreeNode&lt;T&gt; {
<i class="no-highlight">20</i>&nbsp;
<i class="no-highlight">21</i>&nbsp;	/**
<i class="no-highlight">22</i>&nbsp;	 * Creates an instance of this class
<i class="no-highlight">23</i>&nbsp;	 *
<i class="no-highlight">24</i>&nbsp;	 * @param data data to store in the current tree node
<i class="no-highlight">25</i>&nbsp;	 */
<i class="no-highlight">26</i>&nbsp;	public MultiTreeNode(T data) {
<b class="fc"><i class="no-highlight">27</i>&nbsp;		super(data);</b>
<i class="no-highlight">28</i>&nbsp;	}
<i class="no-highlight">29</i>&nbsp;
<i class="no-highlight">30</i>&nbsp;	/**
<i class="no-highlight">31</i>&nbsp;	 * Adds the collection of the subtrees with all of theirs descendants
<i class="no-highlight">32</i>&nbsp;	 * to the current tree node
<i class="no-highlight">33</i>&nbsp;	 * &lt;p&gt;
<i class="no-highlight">34</i>&nbsp;	 * Checks whether this tree node was changed as a result of the call
<i class="no-highlight">35</i>&nbsp;	 *
<i class="no-highlight">36</i>&nbsp;	 * @param subtrees collection of the subtrees with all of their
<i class="no-highlight">37</i>&nbsp;	 *                 descendants
<i class="no-highlight">38</i>&nbsp;	 * @return {@code true} if this tree node was changed as a
<i class="no-highlight">39</i>&nbsp;	 *         result of the call; {@code false} otherwise
<i class="no-highlight">40</i>&nbsp;	 */
<i class="no-highlight">41</i>&nbsp;	public boolean addSubtrees(Collection&lt;? extends MultiTreeNode&lt;T&gt;&gt; subtrees) {
<b class="nc"><i class="no-highlight">42</i>&nbsp;		if (areAllNulls(subtrees)) {</b>
<b class="nc"><i class="no-highlight">43</i>&nbsp;			return false;</b>
<i class="no-highlight">44</i>&nbsp;		}
<b class="nc"><i class="no-highlight">45</i>&nbsp;		for (MultiTreeNode&lt;T&gt; subtree : subtrees) {</b>
<b class="nc"><i class="no-highlight">46</i>&nbsp;			linkParent(subtree, this);</b>
<b class="nc"><i class="no-highlight">47</i>&nbsp;			if (!add(subtree)) {</b>
<b class="nc"><i class="no-highlight">48</i>&nbsp;				return false;</b>
<i class="no-highlight">49</i>&nbsp;			}
<b class="nc"><i class="no-highlight">50</i>&nbsp;		}</b>
<b class="nc"><i class="no-highlight">51</i>&nbsp;		return true;</b>
<i class="no-highlight">52</i>&nbsp;	}
<i class="no-highlight">53</i>&nbsp;
<i class="no-highlight">54</i>&nbsp;	/**
<i class="no-highlight">55</i>&nbsp;	 * Returns the collection of nodes, which have the same parent
<i class="no-highlight">56</i>&nbsp;	 * as the current node; {@link Collections#emptyList()} if the current
<i class="no-highlight">57</i>&nbsp;	 * tree node is root or if the current tree node has no subtrees
<i class="no-highlight">58</i>&nbsp;	 *
<i class="no-highlight">59</i>&nbsp;	 * @return collection of nodes, which have the same parent as
<i class="no-highlight">60</i>&nbsp;	 *         the current node; {@link Collections#emptyList()} if the
<i class="no-highlight">61</i>&nbsp;	 *         current tree node is root or if the current tree node has
<i class="no-highlight">62</i>&nbsp;	 *         no subtrees
<i class="no-highlight">63</i>&nbsp;	 */
<i class="no-highlight">64</i>&nbsp;	public Collection&lt;? extends MultiTreeNode&lt;T&gt;&gt; siblings() {
<b class="nc"><i class="no-highlight">65</i>&nbsp;		if (isRoot()) {</b>
<b class="nc"><i class="no-highlight">66</i>&nbsp;			String message = String.format(&quot;Unable to find the siblings. The tree node %1$s is root&quot;, root());</b>
<b class="nc"><i class="no-highlight">67</i>&nbsp;			throw new TreeNodeException(message);</b>
<i class="no-highlight">68</i>&nbsp;		}
<b class="nc"><i class="no-highlight">69</i>&nbsp;		Collection&lt;? extends TreeNode&lt;T&gt;&gt; parentSubtrees = parent.subtrees();</b>
<b class="nc"><i class="no-highlight">70</i>&nbsp;		int parentSubtreesSize = parentSubtrees.size();</b>
<b class="nc"><i class="no-highlight">71</i>&nbsp;		if (parentSubtreesSize == 1) {</b>
<b class="nc"><i class="no-highlight">72</i>&nbsp;			return Collections.emptySet();</b>
<i class="no-highlight">73</i>&nbsp;		}
<b class="nc"><i class="no-highlight">74</i>&nbsp;		Collection&lt;MultiTreeNode&lt;T&gt;&gt; siblings = new HashSet&lt;&gt;(parentSubtreesSize - 1);</b>
<b class="nc"><i class="no-highlight">75</i>&nbsp;		for (TreeNode&lt;T&gt; parentSubtree : parentSubtrees) {</b>
<b class="nc"><i class="no-highlight">76</i>&nbsp;			if (!parentSubtree.equals(this)) {</b>
<b class="nc"><i class="no-highlight">77</i>&nbsp;				siblings.add((MultiTreeNode&lt;T&gt;) parentSubtree);</b>
<i class="no-highlight">78</i>&nbsp;			}
<b class="nc"><i class="no-highlight">79</i>&nbsp;		}</b>
<b class="nc"><i class="no-highlight">80</i>&nbsp;		return siblings;</b>
<i class="no-highlight">81</i>&nbsp;	}
<i class="no-highlight">82</i>&nbsp;
<i class="no-highlight">83</i>&nbsp;	/**
<i class="no-highlight">84</i>&nbsp;	 * Checks whether among the current tree node subtrees there are
<i class="no-highlight">85</i>&nbsp;	 * all of the subtrees from the specified collection
<i class="no-highlight">86</i>&nbsp;	 *
<i class="no-highlight">87</i>&nbsp;	 * @param subtrees collection of subtrees to be checked for containment
<i class="no-highlight">88</i>&nbsp;	 *                 within the current tree node subtrees
<i class="no-highlight">89</i>&nbsp;	 * @return {@code true} if among the current tree node subtrees
<i class="no-highlight">90</i>&nbsp;	 *         there are all of the subtrees from the specified collection;
<i class="no-highlight">91</i>&nbsp;	 *         {@code false} otherwise
<i class="no-highlight">92</i>&nbsp;	 */
<i class="no-highlight">93</i>&nbsp;	public boolean hasSubtrees(Collection&lt;? extends MultiTreeNode&lt;T&gt;&gt; subtrees) {
<b class="nc"><i class="no-highlight">94</i>&nbsp;		if (isLeaf()</b>
<b class="nc"><i class="no-highlight">95</i>&nbsp;				|| areAllNulls(subtrees)) {</b>
<b class="nc"><i class="no-highlight">96</i>&nbsp;			return false;</b>
<i class="no-highlight">97</i>&nbsp;		}
<b class="nc"><i class="no-highlight">98</i>&nbsp;		for (MultiTreeNode&lt;T&gt; subtree : subtrees) {</b>
<b class="nc"><i class="no-highlight">99</i>&nbsp;			if (!this.hasSubtree(subtree)) {</b>
<b class="nc"><i class="no-highlight">100</i>&nbsp;				return false;</b>
<i class="no-highlight">101</i>&nbsp;			}
<b class="nc"><i class="no-highlight">102</i>&nbsp;		}</b>
<b class="nc"><i class="no-highlight">103</i>&nbsp;		return true;</b>
<i class="no-highlight">104</i>&nbsp;	}
<i class="no-highlight">105</i>&nbsp;
<i class="no-highlight">106</i>&nbsp;	/**
<i class="no-highlight">107</i>&nbsp;	 * Removes all of the collection&#39;s subtrees from the current tree node
<i class="no-highlight">108</i>&nbsp;	 * &lt;p&gt;
<i class="no-highlight">109</i>&nbsp;	 * Checks whether the current tree node was changed as a result of
<i class="no-highlight">110</i>&nbsp;	 * the call
<i class="no-highlight">111</i>&nbsp;	 *
<i class="no-highlight">112</i>&nbsp;	 * @param subtrees collection containing subtrees to be removed from the
<i class="no-highlight">113</i>&nbsp;	 *                 current tree node
<i class="no-highlight">114</i>&nbsp;	 * @return {@code true} if the current tree node was changed as a result
<i class="no-highlight">115</i>&nbsp;	 *         of the call; {@code false} otherwise
<i class="no-highlight">116</i>&nbsp;	 */
<i class="no-highlight">117</i>&nbsp;	public boolean dropSubtrees(Collection&lt;? extends MultiTreeNode&lt;T&gt;&gt; subtrees) {
<b class="nc"><i class="no-highlight">118</i>&nbsp;		if (isLeaf()</b>
<b class="nc"><i class="no-highlight">119</i>&nbsp;				|| areAllNulls(subtrees)) {</b>
<b class="nc"><i class="no-highlight">120</i>&nbsp;			return false;</b>
<i class="no-highlight">121</i>&nbsp;		}
<b class="nc"><i class="no-highlight">122</i>&nbsp;		boolean result = false;</b>
<b class="nc"><i class="no-highlight">123</i>&nbsp;		for (MultiTreeNode&lt;T&gt; subtree : subtrees) {</b>
<b class="nc"><i class="no-highlight">124</i>&nbsp;			boolean currentResult = dropSubtree(subtree);</b>
<b class="nc"><i class="no-highlight">125</i>&nbsp;			if (!result &amp;&amp; currentResult) {</b>
<b class="nc"><i class="no-highlight">126</i>&nbsp;				result = true;</b>
<i class="no-highlight">127</i>&nbsp;			}
<b class="nc"><i class="no-highlight">128</i>&nbsp;		}</b>
<b class="nc"><i class="no-highlight">129</i>&nbsp;		return result;</b>
<i class="no-highlight">130</i>&nbsp;	}
<i class="no-highlight">131</i>&nbsp;
<i class="no-highlight">132</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2021-12-13 00:09</div>
</div>
</body>
</html>
